home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-05-26 | 9.1 KB | 146 lines | [TEXT/MMCC] |
- //------------------------------------------------------------------------------
- // File: event.cp
- // Date: 7/14/94
- // Author: Bretton Wade
- //
- // Description: this file contains definitions related to Macintosh event
- // processing
- //
- //------------------------------------------------------------------------------
-
- #include "event.h"
- #include "menu.h"
- #include "window.h"
-
- //------------------------------------------------------------------------------
- // variables
- //------------------------------------------------------------------------------
- RgnHandle gMouseRgn; // the mouse moved region
- long gSleep = 0; // how long to sleep between events
-
- //------------------------------------------------------------------------------
- // Check whether or not a key is pressed
- //------------------------------------------------------------------------------
- bool KeyIsPressed (ushort key) // check to see if a key is pressed
- { // begin
- uchar keymap[16]; // keymap
- GetKeys ((ulong *) keymap); // get the current keymap
- return bool ((keymap[key >> 3] >> (key & 7)) & 1); // return whether or not the requested key is down
- } // end
-
- //------------------------------------------------------------------------------
- // Handle Mouse Click
- //------------------------------------------------------------------------------
- static void HandleClick (EventRecord &event) // deal with a mouse action
- { // begin
- WindowPtr wind; // window in which event occured
- short part = FindWindow (event.where, &wind); // find the window, and what action
- switch (part) // chech what part of the screen was clicked
- { // begin
- case inMenuBar: // the click was in the menu bar
- AdjustMenus (); // be sure the menus are properly adjusted
- HandleMenu (MenuSelect (event.where)); // deal with the menu event
- break; // end menu bar click
- case inSysWindow: // the click was in a system window
- SystemClick (&event, wind); // let the system handle it
- break; // end system window
- default: // otherwise, the event is a window event
- ((window *) GetWRefCon (wind))->HandleClick (event); // let the window handle it
- break; // end window click
- } // end
- } // end
-
- //------------------------------------------------------------------------------
- // Handle Key Click
- //------------------------------------------------------------------------------
- static void HandleKey (EventRecord &event) // deal with a key action
- { // begin
- if (event.modifiers & cmdKey) // if the command key is down
- if (HandleMenu (MenuKey (event.message & charCodeMask))) // this is a menu event
- return; // we're finished
- WindowPtr wind = MyFrontWindow (); // get the front window
- if (wind) // if there is a front window
- ((window *) GetWRefCon (wind))->HandleKey (event); // tell the front window to handle the key event
- } // end
-
- //------------------------------------------------------------------------------
- // HandleOSEvent
- //------------------------------------------------------------------------------
- static void HandleOSEvent (EventRecord &event) // handle an event from the OS
- { // begin
- WindowPtr wind = MyFrontWindow (); // get the front window
- if ((event.message & osEvtMessageMask) == 0x01000000) // if this is a suspend/resume message
- { // begin
- if (event.message & resumeFlag) // if it is a resume message
- { // begin
- if (wind) // if there is a front window
- ((window *) GetWRefCon (wind))->Activate (event); // activate the front window
- gSleep = 0; // set the sleep time to 0
- } // end
- else // otherwise, it is a suspend message
- gSleep = 4; // set the sleep time to allow other apps to run
- } // end
- else // otherwise, this is a mouse moved event
- if (wind) // if there is a front window
- ((window *) GetWRefCon (wind))->AdjustCursor (event); // tell the front window to adjust the cursor
- else // otherwise, there is no front window
- InitCursor (); // make the cursor an arrow
- } // end
-
- //------------------------------------------------------------------------------
- // Process One Event
- //------------------------------------------------------------------------------
- void ProcessOneEvent (void) // get one macintosh event and handle it
- { // begin
- EventRecord event; // event record to be filled in
- WaitNextEvent (everyEvent, &event, gSleep, gMouseRgn); // fill in the event record
- switch (event.what) // take some action based on the event type
- { // begin
- case nullEvent: // there is no event pending
- YieldToAnyThread (); // do idle time processing
- break; // end null event
- case mouseDown: // a mouse click
- //case mouseUp: // a mouse up
- HandleClick (event); // deal with the mouse event
- break; // end mouse event
- case keyDown: // a key press
- case autoKey: // a key held down
- HandleKey (event); // deal with the key event
- break; // end key event
- case activateEvt: // a window needs to be activated/deactivated
- ((window *) GetWRefCon (WindowPtr (event.message)))->Activate (event); // activate/deactivate the window
- break; // end activate
- case updateEvt: // a window needs to be redrawn
- ((window *) GetWRefCon (WindowPtr (event.message)))->Update (event); // redraw the window
- break; // end redraw event
- case osEvt: // a system event
- HandleOSEvent (event); // deal with it
- break; // end system event
- case diskEvt: // the user has inserted a disk
- if (hiword (event.message)) // check to see if there was an error
- { // begin
- Point pt = {0, 0}; // an empty point
- DIBadMount (pt, event.message); // initialize the disk
- } // end
- break; // end disk event
- case kHighLevelEvent: // apple event
- AEProcessAppleEvent (&event); // dispatch the event
- break; // end apple event
- } // end
- } // end
-
- //------------------------------------------------------------------------------
- // MouseRegion
- //------------------------------------------------------------------------------
- void MouseRegion (RgnHandle region) // assign the region to the mouse region
- { // begin
- CopyRgn (region, gMouseRgn); // copy the region to the mouse region
- Point tmpPt, localPt; // places to get region values
- tmpPt = localPt = TopLeft ((*gMouseRgn)->rgnBBox); // get corner of bounding box
- LocalToGlobal (&tmpPt); // convert to global
- SubPt (localPt, &tmpPt); // find difference
- OffsetRgn (gMouseRgn, tmpPt.h, tmpPt.v); // convert the region to global coordinates
- } // end
-
- //------------------------------------------------------------------------------
-